package com.enjoyor.soa.traffic.core.tsms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.tsms.domain.RltRouteDevice;
import com.enjoyor.soa.traffic.core.tsms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.core.tsms.mapper.IRltRouteDeviceMapper;
import com.enjoyor.soa.traffic.core.tsms.service.IRltRouteDeviceService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;

@Service
@Transactional
@SuppressWarnings("rawtypes")
public class RltRouteDeviceService implements IRltRouteDeviceService {

	@Autowired
	private IRltRouteDeviceMapper iRltRouteDeviceMapper;

	@Autowired
	private IPubLogMapper iPubLogMapper;

	@Override
	public Page getRltRouteDevicePage(Page page, String rltDeviceId, String taskId, String routeId, String deviceId,
			String deviceName, String deviceTypeId) {
		// TODO Auto-generated method stub
		Map<String, Object> mapBean = new HashMap<>();
		mapBean.put("rltDeviceId", rltDeviceId);
		mapBean.put("taskId", taskId);
		mapBean.put("routeId", routeId);
		mapBean.put("deviceId", deviceId);
		mapBean.put("deviceName", deviceName);
		mapBean.put("deviceTypeId", deviceTypeId);
		page.setMapBean(mapBean);
		return iRltRouteDeviceMapper.selectPageInfo(page);
	}

	@Override
	public List<RltRouteDevice> getRltRouteDeviceList(String rltDeviceId, String taskId, String routeId,
			String deviceId, String deviceName, String deviceTypeId) {
		// TODO Auto-generated method stub
		Map<String, Object> mapBean = new HashMap<>();
		mapBean.put("rltDeviceId", rltDeviceId);
		mapBean.put("taskId", taskId);
		mapBean.put("routeId", routeId);
		mapBean.put("deviceId", deviceId);
		mapBean.put("deviceName", deviceName);
		mapBean.put("deviceTypeId", deviceTypeId);
		return iRltRouteDeviceMapper.getRltRouteDeviceList(mapBean);
	}

	@Override
	public Boolean insertRltRouteDevice(RltRouteDevice rltRouteDevice, String personName) {
		// TODO Auto-generated method stub
		Boolean flag = true;
		rltRouteDevice.setRltDeviceId("RD" + TimeHelper.getUniqueString());
		String deviceTypeId = rltRouteDevice.getDeviceTypeId();
		if(EnumDicPublic.DEVICE_TYPE_VEDIO.getKey().equals(deviceTypeId)) {
			rltRouteDevice.setDeviceTypeName(EnumDicPublic.DEVICE_TYPE_VEDIO.getValue());
		}else if(EnumDicPublic.DEVICE_TYPE_SIGNAL.getKey().equals(deviceTypeId)) {
			rltRouteDevice.setDeviceTypeName(EnumDicPublic.DEVICE_TYPE_SIGNAL.getValue());
		}else {
			flag = false;
			return flag;
		}
		iRltRouteDeviceMapper.insertSelective(rltRouteDevice);
		//保存日志
		iPubLogMapper.saveNoramlLogInfo(personName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
				String.format("添加关联设备:{%s} 操作成功!", new Object[] { rltRouteDevice.getId() }),
				LogInfoDic.LOG_MODULAR_ENUM.TSMS_RLT_DEVICE_MANAGEMENT.getMessage());
		return flag;
	}

	@Override
	public void updateRltRouteDevice(RltRouteDevice rltRouteDevice, String personName) {
		// TODO Auto-generated method stub
		iRltRouteDeviceMapper.updateByIdSelective(rltRouteDevice);
		iPubLogMapper.saveNoramlLogInfo(personName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
				String.format("修改关联设备:{%s} 操作成功!", new Object[] { rltRouteDevice.getId() }),
				LogInfoDic.LOG_MODULAR_ENUM.TSMS_RLT_DEVICE_MANAGEMENT.getMessage());
	}

	@Override
	public void deleteRltRouteDevice(String rltDeviceId,String personName) {
		// TODO Auto-generated method stub
		iRltRouteDeviceMapper.deleteById(rltDeviceId);
		iPubLogMapper.saveNoramlLogInfo(personName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_DELETE.getCode(),
				String.format("删除关联设备:{%s} 操作成功!", new Object[] { rltDeviceId }),
				LogInfoDic.LOG_MODULAR_ENUM.TSMS_RLT_DEVICE_MANAGEMENT.getMessage());
	}

	@Override
	public Boolean rltRouteDeviceList(List<RltRouteDevice> list, String taskId, String routeId, String deviceTypeId,
			String personName) {
		// TODO Auto-generated method stub
		Boolean flag = true;
		String deviceTypeName = "";
		if(EnumDicPublic.DEVICE_TYPE_VEDIO.getKey().equals(deviceTypeId)) {
			deviceTypeName = EnumDicPublic.DEVICE_TYPE_VEDIO.getValue();
		}else if(EnumDicPublic.DEVICE_TYPE_SIGNAL.getKey().equals(deviceTypeId)) {
			deviceTypeName = EnumDicPublic.DEVICE_TYPE_SIGNAL.getValue();
		}else{
			flag = false;
			return flag;
		}
		//根据线路编号、设备类型编号删除对应关联信息
		Map<String, Object> param = new HashMap<>();
		param.put("taskId", taskId);
		param.put("routeId", routeId);
		param.put("deviceTypeId", deviceTypeId);
		iRltRouteDeviceMapper.deleteRltRouteDeviceByMap(param);
		//添加关联设备
		for (int i = 0; i < list.size(); i++) {
			RltRouteDevice rltRouteDevice = list.get(i);
			rltRouteDevice.setRltDeviceId("RD" + TimeHelper.getUniqueString());
			rltRouteDevice.setTaskId(taskId);
			rltRouteDevice.setRouteId(routeId);
			rltRouteDevice.setDeviceTypeId(deviceTypeId);
			rltRouteDevice.setDeviceTypeName(deviceTypeName);
			iRltRouteDeviceMapper.insertSelective(rltRouteDevice);
		}
		iPubLogMapper.saveNoramlLogInfo(personName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
				String.format("关联设备:{%s} 操作成功!", new Object[] { routeId }),
				LogInfoDic.LOG_MODULAR_ENUM.TSMS_RLT_DEVICE_MANAGEMENT.getMessage());

		return flag;
	}
	
	@Override
	public Boolean rltRouteDeviceListBase(List<RltRouteDevice> list, String personName) {
		// TODO Auto-generated method stub
		Boolean flag = true;
		//添加关联设备
		for (int i = 0; i < list.size(); i++) {
			RltRouteDevice rltRouteDevice = list.get(i);
			rltRouteDevice.setRltDeviceId("RD" + TimeHelper.getUniqueString());
			iRltRouteDeviceMapper.insertSelective(rltRouteDevice);
		}
		iPubLogMapper.saveNoramlLogInfo(personName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
				String.format("关联设备:{%s} 操作成功!", new Object[] { "批量关联设备" }),
				LogInfoDic.LOG_MODULAR_ENUM.TSMS_RLT_DEVICE_MANAGEMENT.getMessage());
		return flag;
	}

	@Override
	public void replaceRouteDevice(RltRouteDevice rltRouteDevice, String rltDeviceId, String personName) {
		// TODO Auto-generated method stub
		//iRltRouteDeviceMapper.deleteById(rltDeviceId);
		//rltRouteDevice.setRltDeviceId("RD" + TimeHelper.getUniqueString());
		//iRltRouteDeviceMapper.insertSelective(rltRouteDevice);
		rltRouteDevice.setRltDeviceId(rltDeviceId);
		iRltRouteDeviceMapper.updateByIdSelective(rltRouteDevice);
		iPubLogMapper.saveNoramlLogInfo(personName, null, LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_UPDATE.getCode(),
				String.format("替换设备:{%s} 操作成功!", new Object[] { rltDeviceId }),
				LogInfoDic.LOG_MODULAR_ENUM.TSMS_RLT_DEVICE_MANAGEMENT.getMessage());
	}

}
